package cn.com.huzo.repository.oa.impl;

import cn.com.huzo.model.entities.oa.MyCalendarEntity;
import cn.com.huzo.repository.oa.MyCalendarRepository;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

/**
 * 我的日程数据访问对象
 *
 * @author 吴旭
 */
@Repository("MyCalendarRepository")
public class MyCalendarRepositoryImpl extends GenericRepositoryImpl<MyCalendarEntity, String>
        implements MyCalendarRepository {

    private final String namespace = "cn.com.huzo.repository.oa.MyCalendarRepository";

    /**
     * 构造函数,设置the instance to MyCalendarRepositoryImpl.class.
     */
    public MyCalendarRepositoryImpl() {
        super(MyCalendarEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public MyCalendarEntity fetchByKey(String key) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByCalendarId"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<MyCalendarEntity> fetchCalendar(Long userId, Date start, Date end) {
        Map<String, Object> params = new HashMap<>();
        params.put("UserId", userId);
        params.put("StartTime", start);
        params.put("EndTime", end);

        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchCalendar"), params);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String insert(MyCalendarEntity instance) {
        getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insert"), instance);

        // 返回出入后的记录ID
        return instance.getCalendarId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer update(MyCalendarEntity instance) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "update"), instance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer delete(String key) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "delete"), key);
    }
}
